package com.xnew.holiday.controller;

import com.xnew.aes.LogParameter;
import com.xnew.bean.common.SysUserInfo;
import com.xnew.holiday.entity.HolidayManage;
import com.xnew.holiday.service.HolidayManageService;
import com.xnew.utils.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping(value = "/holidayManage")
public class HolidayManageController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private HolidayManageService holidayManageService;

    /**
     * 节假日管理列表
     */
    @LogParameter(key = "", desc = "节假日管理列表")
    @RequestMapping(value = "/holidayManageList", method = RequestMethod.POST)
    public DataOutput holidayManageList(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        try{
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            List<HolidayManage> holidayManageList = holidayManageService.holidayManageList(input, userInfo);
            out = MybatisParam.getOutput(ConvertEntityUtil.convertListData(holidayManageList));
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            out.setCode(-93001);
            out.setMsg("查询出错！");
        }
        return out;
    }

    /**
     * 节假日查询
     */
    @LogParameter(key = "", desc = "节假日查询")
    @RequestMapping(value = "/queryHolidayManage", method = RequestMethod.POST)
    public DataOutput queryHolidayManage(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        if (
            input.getParam().get("hmId") == null ||
            StringUtils.isEmpty(input.getParam().get("hmId").toString())
        ){
            out.setCode(-30000);
            out.setMsg("缺少必填参数，请检查后重试");
            return out;
        }
        try{
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            HolidayManage holidayManage = holidayManageService.queryHolidayManage(input.getParam().get("hmId").toString());
            out.setData(ConvertEntityUtil.convertEntityData(holidayManage));
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            out.setCode(-93001);
            out.setMsg("查询出错！");
        }
        return out;
    }

    /**
     * 年份是否存在
     * @param input
     * @param request
     * @return
     */
    @LogParameter(key = "", desc = "年份是否存在")
    @RequestMapping(value = "/whetherYearExists", method = RequestMethod.POST)
    public DataOutput whetherYearExists(@RequestBody DataInput input,HttpServletRequest request){
        DataOutput out = DataOutput.success();
        if (
            input.getParam().get("hmYear") == null ||
            StringUtils.isEmpty(input.getParam().get("hmYear").toString())
        ){
            out.setCode(-30000);
            out.setMsg("缺少必填参数，请检查后重试");
            return out;
        }
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            HolidayManage holidayManage = new HolidayManage();
            holidayManage =(HolidayManage) ConvertEntityUtil.convertEntityByParam(input.getParam(), holidayManage);
            out = holidayManageService.whetherYearExists(holidayManage, userInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            out.setCode(-90020);
            out.setMsg("操作失败");
        }
        return out;
    }

    /**
     * 新增节假日信息
     * @param input
     * @param request
     * @return
     */
    @LogParameter(key = "", desc = "新增节假日信息")
    @RequestMapping(value = "/holidayManageAdd", method = RequestMethod.POST)
    public DataOutput holidayManageAdd(@RequestBody DataInput input,HttpServletRequest request){
        DataOutput out = DataOutput.success();
        if (
            input.getParam().get("hmYear") == null ||
            StringUtils.isEmpty(input.getParam().get("hmYear").toString()) ||
            input.getParam().get("hmDate") == null ||
            StringUtils.isEmpty(input.getParam().get("hmDate").toString())
        ){
            out.setCode(-30000);
            out.setMsg("缺少必填参数，请检查后重试");
            return out;
        }
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            HolidayManage holidayManage = new HolidayManage();
            holidayManage =(HolidayManage) ConvertEntityUtil.convertEntityByParam(input.getParam(), holidayManage);
            holidayManageService.holidayManageAdd(holidayManage, userInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            out.setCode(-90020);
            out.setMsg("操作失败");
        }
        return out;
    }

    /**
     * 修改节假日信息
     * @param input
     * @param request
     * @return
     */
    @LogParameter(key = "", desc = "修改节假日信息")
    @RequestMapping(value = "/holidayManageUpdate", method = RequestMethod.POST)
    public DataOutput holidayManageUpdate(@RequestBody DataInput input,HttpServletRequest request){
        DataOutput out = DataOutput.success();
        if (
            input.getParam().get("hmId") == null ||
            StringUtils.isEmpty(input.getParam().get("hmId").toString()) ||
            input.getParam().get("hmYear") == null ||
            StringUtils.isEmpty(input.getParam().get("hmYear").toString()) ||
            input.getParam().get("hmDate") == null ||
            StringUtils.isEmpty(input.getParam().get("hmDate").toString())
        ){
            out.setCode(-30000);
            out.setMsg("缺少必填参数，请检查后重试");
            return out;
        }
        try {
            SysUserInfo userInfo = UserUtil.getUserInfo(request);
            HolidayManage holidayManage = new HolidayManage();
            holidayManage =(HolidayManage) ConvertEntityUtil.convertEntityByParam(input.getParam(), holidayManage);
            holidayManageService.holidayManageUpdate(holidayManage, userInfo);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            out.setCode(-90020);
            out.setMsg("操作失败");
        }
        return out;
    }

    /**
     * 节假日删除
     */
    @LogParameter(key = "", desc = "节假日删除")
    @RequestMapping(value = "/holidayManageDelete", method = RequestMethod.POST)
    public DataOutput holidayManageDelete(@RequestBody DataInput input, HttpServletRequest request){
        DataOutput out = DataOutput.success();
        if (
            input.getParam().get("hmId") == null ||
            StringUtils.isEmpty(input.getParam().get("hmId").toString())
        ){
            out.setCode(-30000);
            out.setMsg("缺少必填参数，请检查后重试");
            return out;
        }
        try{
            holidayManageService.holidayManageDelete(input.getParam().get("hmId").toString());
        }catch (Exception e){
            logger.error(e.getMessage(), e);
            out.setCode(-93001);
            out.setMsg("查询出错！");
        }
        return out;
    }
}
